package cn.hwadee.muying.service;

import cn.hwadee.muying.commons.base.BaseService;
import cn.hwadee.muying.commons.constant.ConstantRole;
import cn.hwadee.muying.commons.page.MyPage;
import cn.hwadee.muying.commons.utils.DateTools;
import cn.hwadee.muying.commons.utils.Md5Utils;
import cn.hwadee.muying.dto.Client;
import cn.hwadee.muying.entity.ClientInfo;
import cn.hwadee.muying.entity.User;
import cn.hwadee.muying.entity.WorkerInfo;
import cn.hwadee.muying.mapper.ClientMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @创建人 skiner
 * @创建时间 2018/8/6
 * @描述
 */
@Service
public class ClientService{

    protected final Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ClientInfoService clientInfoService;

    @Autowired
    private UserService userService;

    @Autowired
    private ClientMapper clientMapper;

    public MyPage<Client> list(MyPage<Client> page, User cUser,Boolean old,String keyword) throws Exception {
        page.setOrderByField("b.updateDate");
        List<Client> list = clientMapper.list(page,cUser,old,keyword);
        for(Client client:list){
            if(client.getDueDate()!=null){
                client.setDueDateString(DateTools.dayFormat.format(client.getDueDate()));
            }
            if(client.getDemandDate()!=null){
                client.setDemandDateString(DateTools.dayFormat.format(client.getDemandDate()));
            }
            if(client.getBirthday()!=null){
                client.setBirthdayString(DateTools.dayFormat.format(client.getBirthday()));
            }
        }
        page.setRecords(list);
        return page;
    }

    /**
     * 需要cUser的orgId
     * @param client
     * @param cUser
     * @return
     */
    public boolean insert(Client client, User cUser){
        try {
            User user = new User();
            user.setUsername(client.getUsername());
            user.setAccount(client.getAccount());
            user.setPassword(new Md5Utils().getkeyBeanofStr(client.getPassword()));
            user.setRid(ConstantRole.ROLE_CUSTOM);
            user.setOrgId(cUser.getOrgId());
            user.setTelephone(client.getTelephone());
            user.setBirthday(client.getBirthday());
            user.setAddress(client.getAddress());
            user.setEmail(client.getEmail());

            userService.insert(user);

            user = userService.getByAccount(user.getAccount());

            ClientInfo info = new ClientInfo();
            info.setUserId(user.getId());
            info.setPresenterId(client.getPresenterId());
            info.setSource(client.getSource());
            info.setDueDate(client.getDueDate());
            info.setDemandDate(client.getDemandDate());
            info.setDemandContent(client.getDemandContent());
            info.setExpectedCity(client.getExpectedCity());
            info.setOld(client.getOld());
            info.setValid(client.getValid());

            clientInfoService.insert(info);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }





    public void updateById(Client client) throws RuntimeException{
        User user = userService.selectById(client.getUserId());
        user.setUsername(client.getUsername());
//        user.setAccount(client.getAccount());
        user.setRid(client.getRid());
        user.setOrgId(client.getOrgId());
        user.setAddress(client.getAddress());
        user.setBirthday(client.getBirthday());
        user.setEmail(client.getEmail());
        user.setTelephone(client.getTelephone());

        ClientInfo clientInfo = clientInfoService.selectById(client.getId());
        clientInfo.setUserId(client.getUserId());
        clientInfo.setPresenterId(client.getPresenterId());
        clientInfo.setSource(client.getSource());
        clientInfo.setDueDate(client.getDueDate());
        clientInfo.setDemandContent(client.getDemandContent());
        clientInfo.setDemandDate(client.getDemandDate());
        clientInfo.setExpectedCity(client.getExpectedCity());
        clientInfo.setOld(client.getOld());

        userService.updateById(user);
        clientInfoService.updateById(clientInfo);

    }

    /**
     *
     * @param clientInfoId
     * @return
     */
    public Client selectById(Long clientInfoId) {
        ClientInfo info = clientInfoService.selectById(clientInfoId);
        User user = userService.selectById(info.getUserId());
        Client client = new Client();
        client.setUserId(user.getId());
        client.setAccount(user.getAccount());
        client.setUsername(user.getUsername());
        client.setRid(user.getRid());
        client.setOrgId(user.getOrgId());
        client.setTelephone(user.getTelephone());
        client.setAddress(user.getAddress());
        client.setBirthday(user.getBirthday());
        client.setEmail(user.getEmail());

        client.setId(info.getId());
        client.setPresenterId(info.getPresenterId());
        client.setSource(info.getSource());
        client.setDueDate(info.getDueDate());
        client.setDemandDate(info.getDemandDate());
        client.setDemandContent(info.getDemandContent());
        client.setExpectedCity(info.getExpectedCity());
        client.setOld(info.getOld());
        return client;
    }

    /**
     *
     * @param userId
     * @return
     */
    public Client selectByUserId(Long userId) {
        User user=userService.selectById(userId);
        ClientInfo info = clientInfoService.selectByUserId(userId);
        Client client = new Client();
        if(user!=null){
            client.setUserId(user.getId());
            client.setAccount(user.getAccount());
            client.setUsername(user.getUsername());
            client.setRid(user.getRid());
            client.setOrgId(user.getOrgId());
            client.setTelephone(user.getTelephone());
            client.setAddress(user.getAddress());
            client.setBirthday(user.getBirthday());
            client.setEmail(user.getEmail());
        }
        if(info!=null){
            client.setId(info.getId());
            client.setPresenterId(info.getPresenterId());
            client.setSource(info.getSource());
            client.setDueDate(info.getDueDate());
            client.setDemandDate(info.getDemandDate());
            client.setDemandContent(info.getDemandContent());
            client.setExpectedCity(info.getExpectedCity());
            client.setOld(info.getOld());
        }
        return client;
    }


    public void deleteById(Long clientInfoId){
        ClientInfo info = clientInfoService.selectById(clientInfoId);
        userService.deleteById(info.getUserId());
        clientInfoService.deleteById(info.getId());
    }

    public Client selectByAccount(String account) throws Exception {
        return clientMapper.selectByAccount(account);
    }

}
